package com.stylefeng.guns.modular.verbal.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.SuccessTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.system.model.Pic;
import com.stylefeng.guns.modular.verbal.dao.PicDao;
import com.stylefeng.guns.modular.verbal.service.IPicService;
import com.stylefeng.guns.modular.verbal.warpper.PicWarpper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 图片控制器
 *
 * @author fengshuonan
 * @Date 2018-04-12 15:49:36
 */
@Controller
@RequestMapping("/pic")
public class PicController extends BaseController {

    private String PREFIX = "/verbal/pic/";

    @Autowired
    private IPicService picService;

    @Autowired
    private PicDao picDao;

    /**
     * 跳转到图片首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "pic.html";
    }

    /**
     * 跳转到添加图片
     */
    @RequestMapping("/pic_add")
    public String picAdd() {
        return PREFIX + "pic_add.html";
    }

    /**
     * 跳转到修改图片
     */
    @RequestMapping("/pic_update/{picId}")
    public String picUpdate(@PathVariable Integer picId, Model model) {
        Pic pic = picService.selectById(picId);
        Map<String, Object> map = BeanKit.beanToMap(pic);
        model.addAttribute("item", new PicWarpper(map).warp());
        LogObjectHolder.me().set(pic);
        return PREFIX + "pic_edit.html";
    }

    /**
     * 获取图片列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "typeCode", required = false) String typeCode
            , @RequestParam(value = "title", required = false) String title) {
        Page<Pic> page = new PageFactory<Pic>().defaultPage();

        List<Map<String, Object>> list = picDao.selectPics(page, title, typeCode);
        page.setRecords((List<Pic>) new PicWarpper(list).warp());
        return super.packForBT(page);
    }

    /**
     * 新增图片
     */
    @ApiOperation(value = "新增图片", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "title", value = "标题", dataType = "string", required = true),
                    @ApiImplicitParam(name = "pic", value = "图片url", dataType = "string", required = true),
                    @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true)
            }
    )
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid Pic pic) {
        pic.setCreateDate(new Date());
        picService.insert(pic);
        return SUCCESS_TIP;
    }

    /**
     * 删除图片
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer picId) {
        picService.deleteById(picId);
        return SUCCESS_TIP;
    }

    /**
     * 修改图片
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "更新图片", tags = "话术", httpMethod = "POST", code = 200, response = SuccessTip.class)
    @ApiImplicitParams(
        {
            @ApiImplicitParam(name = "id", value = "id", dataType = "integer", required = true),
            @ApiImplicitParam(name = "title", value = "标题", dataType = "string", required = true),
            @ApiImplicitParam(name = "pic", value = "图片url", dataType = "string", required = true),
            @ApiImplicitParam(name = "typeCode", value = "话术分类code,从字典中获取，对应字典的num", dataType = "string", required = true)
        }
    )
    public Object update(@Valid Pic pic) {
        picService.updateById(pic);
        return SUCCESS_TIP;
    }

    /**
     * 图片详情
     */
    @RequestMapping(value = "/detail/{picId}")
    @ResponseBody
    public Object detail(@PathVariable("picId") Integer picId) {
        return picService.selectById(picId);
    }
}
